{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tips for Selecting Columns in a DataFrame\n",
    "\n",
    "Notebook to accompany this [post](https://pbpython.com/selecting-columns.html).\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(\n",
    "    'https://data.cityofnewyork.us/api/views/vfnx-vebw/rows.csv?accessType=DOWNLOAD&bom=true&format=true'\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Build a mapping list so we can see the index of all the columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "col_mapping = [f\"{c[0]}:{c[1]}\" for c in enumerate(df.columns)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['0:X',\n",
       " '1:Y',\n",
       " '2:Unique Squirrel ID',\n",
       " '3:Hectare',\n",
       " '4:Shift',\n",
       " '5:Date',\n",
       " '6:Hectare Squirrel Number',\n",
       " '7:Age',\n",
       " '8:Primary Fur Color',\n",
       " '9:Highlight Fur Color',\n",
       " '10:Combination of Primary and Highlight Color',\n",
       " '11:Color notes',\n",
       " '12:Location',\n",
       " '13:Above Ground Sighter Measurement',\n",
       " '14:Specific Location',\n",
       " '15:Running',\n",
       " '16:Chasing',\n",
       " '17:Climbing',\n",
       " '18:Eating',\n",
       " '19:Foraging',\n",
       " '20:Other Activities',\n",
       " '21:Kuks',\n",
       " '22:Quaas',\n",
       " '23:Moans',\n",
       " '24:Tail flags',\n",
       " '25:Tail twitches',\n",
       " '26:Approaches',\n",
       " '27:Indifferent',\n",
       " '28:Runs from',\n",
       " '29:Other Interactions',\n",
       " '30:Lat/Long',\n",
       " '31:Zip Codes',\n",
       " '32:Community Districts',\n",
       " '33:Borough Boundaries',\n",
       " '34:City Council Districts',\n",
       " '35:Police Precincts']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "col_mapping"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also build a dictionary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "col_mapping_dict = {c[0]:c[1] for c in enumerate(df.columns)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0: 'X',\n",
       " 1: 'Y',\n",
       " 2: 'Unique Squirrel ID',\n",
       " 3: 'Hectare',\n",
       " 4: 'Shift',\n",
       " 5: 'Date',\n",
       " 6: 'Hectare Squirrel Number',\n",
       " 7: 'Age',\n",
       " 8: 'Primary Fur Color',\n",
       " 9: 'Highlight Fur Color',\n",
       " 10: 'Combination of Primary and Highlight Color',\n",
       " 11: 'Color notes',\n",
       " 12: 'Location',\n",
       " 13: 'Above Ground Sighter Measurement',\n",
       " 14: 'Specific Location',\n",
       " 15: 'Running',\n",
       " 16: 'Chasing',\n",
       " 17: 'Climbing',\n",
       " 18: 'Eating',\n",
       " 19: 'Foraging',\n",
       " 20: 'Other Activities',\n",
       " 21: 'Kuks',\n",
       " 22: 'Quaas',\n",
       " 23: 'Moans',\n",
       " 24: 'Tail flags',\n",
       " 25: 'Tail twitches',\n",
       " 26: 'Approaches',\n",
       " 27: 'Indifferent',\n",
       " 28: 'Runs from',\n",
       " 29: 'Other Interactions',\n",
       " 30: 'Lat/Long',\n",
       " 31: 'Zip Codes',\n",
       " 32: 'Community Districts',\n",
       " 33: 'Borough Boundaries',\n",
       " 34: 'City Council Districts',\n",
       " 35: 'Police Precincts'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "col_mapping_dict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use iloc to select just the second column (Unique Squirrel ID)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       37F-PM-1014-03\n",
       "1       37E-PM-1006-03\n",
       "2        2E-AM-1010-03\n",
       "3        5D-PM-1018-05\n",
       "4       39B-AM-1018-01\n",
       "             ...      \n",
       "3018    30B-AM-1007-04\n",
       "3019    19A-PM-1013-05\n",
       "3020    22D-PM-1012-07\n",
       "3021    29B-PM-1010-02\n",
       "3022     5E-PM-1012-01\n",
       "Name: Unique Squirrel ID, Length: 3023, dtype: object"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[:, 2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Pass a list of integers to select multiple columns by index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>X</th>\n",
       "      <th>Y</th>\n",
       "      <th>Unique Squirrel ID</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>-73.956134</td>\n",
       "      <td>40.794082</td>\n",
       "      <td>37F-PM-1014-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>-73.957044</td>\n",
       "      <td>40.794851</td>\n",
       "      <td>37E-PM-1006-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>-73.976831</td>\n",
       "      <td>40.766718</td>\n",
       "      <td>2E-AM-1010-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>-73.975725</td>\n",
       "      <td>40.769703</td>\n",
       "      <td>5D-PM-1018-05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>-73.959313</td>\n",
       "      <td>40.797533</td>\n",
       "      <td>39B-AM-1018-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3018</td>\n",
       "      <td>-73.963943</td>\n",
       "      <td>40.790868</td>\n",
       "      <td>30B-AM-1007-04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3019</td>\n",
       "      <td>-73.970402</td>\n",
       "      <td>40.782560</td>\n",
       "      <td>19A-PM-1013-05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3020</td>\n",
       "      <td>-73.966587</td>\n",
       "      <td>40.783678</td>\n",
       "      <td>22D-PM-1012-07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3021</td>\n",
       "      <td>-73.963994</td>\n",
       "      <td>40.789915</td>\n",
       "      <td>29B-PM-1010-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3022</td>\n",
       "      <td>-73.975479</td>\n",
       "      <td>40.769640</td>\n",
       "      <td>5E-PM-1012-01</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3023 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "              X          Y Unique Squirrel ID\n",
       "0    -73.956134  40.794082     37F-PM-1014-03\n",
       "1    -73.957044  40.794851     37E-PM-1006-03\n",
       "2    -73.976831  40.766718      2E-AM-1010-03\n",
       "3    -73.975725  40.769703      5D-PM-1018-05\n",
       "4    -73.959313  40.797533     39B-AM-1018-01\n",
       "...         ...        ...                ...\n",
       "3018 -73.963943  40.790868     30B-AM-1007-04\n",
       "3019 -73.970402  40.782560     19A-PM-1013-05\n",
       "3020 -73.966587  40.783678     22D-PM-1012-07\n",
       "3021 -73.963994  40.789915     29B-PM-1010-02\n",
       "3022 -73.975479  40.769640      5E-PM-1012-01\n",
       "\n",
       "[3023 rows x 3 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[:, [0,1,2]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also pass a slice object to select a range of columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>X</th>\n",
       "      <th>Y</th>\n",
       "      <th>Unique Squirrel ID</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>-73.956134</td>\n",
       "      <td>40.794082</td>\n",
       "      <td>37F-PM-1014-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>-73.957044</td>\n",
       "      <td>40.794851</td>\n",
       "      <td>37E-PM-1006-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>-73.976831</td>\n",
       "      <td>40.766718</td>\n",
       "      <td>2E-AM-1010-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>-73.975725</td>\n",
       "      <td>40.769703</td>\n",
       "      <td>5D-PM-1018-05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>-73.959313</td>\n",
       "      <td>40.797533</td>\n",
       "      <td>39B-AM-1018-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3018</td>\n",
       "      <td>-73.963943</td>\n",
       "      <td>40.790868</td>\n",
       "      <td>30B-AM-1007-04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3019</td>\n",
       "      <td>-73.970402</td>\n",
       "      <td>40.782560</td>\n",
       "      <td>19A-PM-1013-05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3020</td>\n",
       "      <td>-73.966587</td>\n",
       "      <td>40.783678</td>\n",
       "      <td>22D-PM-1012-07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3021</td>\n",
       "      <td>-73.963994</td>\n",
       "      <td>40.789915</td>\n",
       "      <td>29B-PM-1010-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3022</td>\n",
       "      <td>-73.975479</td>\n",
       "      <td>40.769640</td>\n",
       "      <td>5E-PM-1012-01</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3023 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "              X          Y Unique Squirrel ID\n",
       "0    -73.956134  40.794082     37F-PM-1014-03\n",
       "1    -73.957044  40.794851     37E-PM-1006-03\n",
       "2    -73.976831  40.766718      2E-AM-1010-03\n",
       "3    -73.975725  40.769703      5D-PM-1018-05\n",
       "4    -73.959313  40.797533     39B-AM-1018-01\n",
       "...         ...        ...                ...\n",
       "3018 -73.963943  40.790868     30B-AM-1007-04\n",
       "3019 -73.970402  40.782560     19A-PM-1013-05\n",
       "3020 -73.966587  40.783678     22D-PM-1012-07\n",
       "3021 -73.963994  40.789915     29B-PM-1010-02\n",
       "3022 -73.975479  40.769640      5E-PM-1012-01\n",
       "\n",
       "[3023 rows x 3 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[:, 0:3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we want to combine the list and slice notation, we need to use nump.r_ to process the data into an appropriate format."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2, 15, 16, 17, 18, 24, 25])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.r_[0:3,15:19,24,25]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can pass the output of np.r_ to .iloc to use multiple selection approaches"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>X</th>\n",
       "      <th>Y</th>\n",
       "      <th>Unique Squirrel ID</th>\n",
       "      <th>Running</th>\n",
       "      <th>Chasing</th>\n",
       "      <th>Climbing</th>\n",
       "      <th>Eating</th>\n",
       "      <th>Tail flags</th>\n",
       "      <th>Tail twitches</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>-73.956134</td>\n",
       "      <td>40.794082</td>\n",
       "      <td>37F-PM-1014-03</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>-73.957044</td>\n",
       "      <td>40.794851</td>\n",
       "      <td>37E-PM-1006-03</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>-73.976831</td>\n",
       "      <td>40.766718</td>\n",
       "      <td>2E-AM-1010-03</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>-73.975725</td>\n",
       "      <td>40.769703</td>\n",
       "      <td>5D-PM-1018-05</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>-73.959313</td>\n",
       "      <td>40.797533</td>\n",
       "      <td>39B-AM-1018-01</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3018</td>\n",
       "      <td>-73.963943</td>\n",
       "      <td>40.790868</td>\n",
       "      <td>30B-AM-1007-04</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3019</td>\n",
       "      <td>-73.970402</td>\n",
       "      <td>40.782560</td>\n",
       "      <td>19A-PM-1013-05</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3020</td>\n",
       "      <td>-73.966587</td>\n",
       "      <td>40.783678</td>\n",
       "      <td>22D-PM-1012-07</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3021</td>\n",
       "      <td>-73.963994</td>\n",
       "      <td>40.789915</td>\n",
       "      <td>29B-PM-1010-02</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3022</td>\n",
       "      <td>-73.975479</td>\n",
       "      <td>40.769640</td>\n",
       "      <td>5E-PM-1012-01</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3023 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "              X          Y Unique Squirrel ID  Running  Chasing  Climbing  \\\n",
       "0    -73.956134  40.794082     37F-PM-1014-03    False    False     False   \n",
       "1    -73.957044  40.794851     37E-PM-1006-03     True    False     False   \n",
       "2    -73.976831  40.766718      2E-AM-1010-03    False    False      True   \n",
       "3    -73.975725  40.769703      5D-PM-1018-05    False    False      True   \n",
       "4    -73.959313  40.797533     39B-AM-1018-01    False    False     False   \n",
       "...         ...        ...                ...      ...      ...       ...   \n",
       "3018 -73.963943  40.790868     30B-AM-1007-04    False    False     False   \n",
       "3019 -73.970402  40.782560     19A-PM-1013-05    False    False     False   \n",
       "3020 -73.966587  40.783678     22D-PM-1012-07    False    False     False   \n",
       "3021 -73.963994  40.789915     29B-PM-1010-02    False    False     False   \n",
       "3022 -73.975479  40.769640      5E-PM-1012-01    False    False     False   \n",
       "\n",
       "      Eating  Tail flags  Tail twitches  \n",
       "0      False       False          False  \n",
       "1      False       False          False  \n",
       "2      False       False          False  \n",
       "3      False       False          False  \n",
       "4      False       False          False  \n",
       "...      ...         ...            ...  \n",
       "3018    True       False          False  \n",
       "3019   False       False          False  \n",
       "3020    True       False          False  \n",
       "3021    True       False          False  \n",
       "3022    True       False          False  \n",
       "\n",
       "[3023 rows x 9 columns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[:, np.r_[0:3,15:19,24,25]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can use the same notation when reading in a csv as well"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_2 = pd.read_csv(\n",
    "    'https://data.cityofnewyork.us/api/views/vfnx-vebw/rows.csv?accessType=DOWNLOAD&bom=true&format=true',\n",
    "    usecols=np.r_[1,2,5:8,15:25],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Y</th>\n",
       "      <th>Unique Squirrel ID</th>\n",
       "      <th>Date</th>\n",
       "      <th>Hectare Squirrel Number</th>\n",
       "      <th>Age</th>\n",
       "      <th>Running</th>\n",
       "      <th>Chasing</th>\n",
       "      <th>Climbing</th>\n",
       "      <th>Eating</th>\n",
       "      <th>Foraging</th>\n",
       "      <th>Other Activities</th>\n",
       "      <th>Kuks</th>\n",
       "      <th>Quaas</th>\n",
       "      <th>Moans</th>\n",
       "      <th>Tail flags</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>40.794082</td>\n",
       "      <td>37F-PM-1014-03</td>\n",
       "      <td>10142018</td>\n",
       "      <td>3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>40.794851</td>\n",
       "      <td>37E-PM-1006-03</td>\n",
       "      <td>10062018</td>\n",
       "      <td>3</td>\n",
       "      <td>Adult</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>40.766718</td>\n",
       "      <td>2E-AM-1010-03</td>\n",
       "      <td>10102018</td>\n",
       "      <td>3</td>\n",
       "      <td>Adult</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>40.769703</td>\n",
       "      <td>5D-PM-1018-05</td>\n",
       "      <td>10182018</td>\n",
       "      <td>5</td>\n",
       "      <td>Juvenile</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>40.797533</td>\n",
       "      <td>39B-AM-1018-01</td>\n",
       "      <td>10182018</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>unknown</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           Y Unique Squirrel ID      Date  Hectare Squirrel Number       Age  \\\n",
       "0  40.794082     37F-PM-1014-03  10142018                        3       NaN   \n",
       "1  40.794851     37E-PM-1006-03  10062018                        3     Adult   \n",
       "2  40.766718      2E-AM-1010-03  10102018                        3     Adult   \n",
       "3  40.769703      5D-PM-1018-05  10182018                        5  Juvenile   \n",
       "4  40.797533     39B-AM-1018-01  10182018                        1       NaN   \n",
       "\n",
       "   Running  Chasing  Climbing  Eating  Foraging Other Activities   Kuks  \\\n",
       "0    False    False     False   False     False              NaN  False   \n",
       "1     True    False     False   False     False              NaN  False   \n",
       "2    False    False      True   False     False              NaN  False   \n",
       "3    False    False      True   False     False              NaN  False   \n",
       "4    False    False     False   False     False          unknown   True   \n",
       "\n",
       "   Quaas  Moans  Tail flags  \n",
       "0  False  False       False  \n",
       "1  False  False       False  \n",
       "2  False  False       False  \n",
       "3  False  False       False  \n",
       "4  False  False       False  "
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_2.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also select columns using a boolean array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False, False, False, False, False, False, False,\n",
       "       False, False, False, False, False, False,  True, False, False,\n",
       "       False, False, False, False, False, False, False, False, False,\n",
       "       False,  True, False, False, False, False, False, False, False])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "run_cols = df.columns.str.contains('run', case=False)\n",
    "run_cols"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Running</th>\n",
       "      <th>Runs from</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Running  Runs from\n",
       "0    False      False\n",
       "1     True       True\n",
       "2    False      False\n",
       "3    False       True\n",
       "4    False      False"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[:, run_cols].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A lambda function can be useful for combining into 1 line."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Running</th>\n",
       "      <th>Runs from</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Running  Runs from\n",
       "0    False      False\n",
       "1     True       True\n",
       "2    False      False\n",
       "3    False       True\n",
       "4    False      False"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[:, lambda df:df.columns.str.contains('run', case=False)].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A more complex example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Community Districts</th>\n",
       "      <th>Borough Boundaries</th>\n",
       "      <th>City Council Districts</th>\n",
       "      <th>Police Precincts</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>19</td>\n",
       "      <td>4</td>\n",
       "      <td>19</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>19</td>\n",
       "      <td>4</td>\n",
       "      <td>19</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>19</td>\n",
       "      <td>4</td>\n",
       "      <td>19</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>19</td>\n",
       "      <td>4</td>\n",
       "      <td>19</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>19</td>\n",
       "      <td>4</td>\n",
       "      <td>19</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Community Districts  Borough Boundaries  City Council Districts  \\\n",
       "0                   19                   4                      19   \n",
       "1                   19                   4                      19   \n",
       "2                   19                   4                      19   \n",
       "3                   19                   4                      19   \n",
       "4                   19                   4                      19   \n",
       "\n",
       "   Police Precincts  \n",
       "0                13  \n",
       "1                13  \n",
       "2                13  \n",
       "3                13  \n",
       "4                13  "
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[:, lambda df: df.columns.str.contains('district|precinct|boundaries',\n",
    "                                              case=False)].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Combining index and boolean arrays"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False, False, False, False, False, False, False,\n",
       "       False, False, False, False, False, False, False, False, False,\n",
       "       False, False, False, False, False, False, False, False, False,\n",
       "       False, False, False, False, False,  True,  True,  True,  True])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "location_cols = df.columns.str.contains('district|precinct|boundaries',\n",
    "                                        case=False)\n",
    "location_cols"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[32, 33, 34, 35]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "location_indices = [i for i, col in enumerate(location_cols) if col]\n",
    "location_indices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>X</th>\n",
       "      <th>Y</th>\n",
       "      <th>Unique Squirrel ID</th>\n",
       "      <th>Community Districts</th>\n",
       "      <th>Borough Boundaries</th>\n",
       "      <th>City Council Districts</th>\n",
       "      <th>Police Precincts</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>-73.956134</td>\n",
       "      <td>40.794082</td>\n",
       "      <td>37F-PM-1014-03</td>\n",
       "      <td>19</td>\n",
       "      <td>4</td>\n",
       "      <td>19</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>-73.957044</td>\n",
       "      <td>40.794851</td>\n",
       "      <td>37E-PM-1006-03</td>\n",
       "      <td>19</td>\n",
       "      <td>4</td>\n",
       "      <td>19</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>-73.976831</td>\n",
       "      <td>40.766718</td>\n",
       "      <td>2E-AM-1010-03</td>\n",
       "      <td>19</td>\n",
       "      <td>4</td>\n",
       "      <td>19</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>-73.975725</td>\n",
       "      <td>40.769703</td>\n",
       "      <td>5D-PM-1018-05</td>\n",
       "      <td>19</td>\n",
       "      <td>4</td>\n",
       "      <td>19</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>-73.959313</td>\n",
       "      <td>40.797533</td>\n",
       "      <td>39B-AM-1018-01</td>\n",
       "      <td>19</td>\n",
       "      <td>4</td>\n",
       "      <td>19</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           X          Y Unique Squirrel ID  Community Districts  \\\n",
       "0 -73.956134  40.794082     37F-PM-1014-03                   19   \n",
       "1 -73.957044  40.794851     37E-PM-1006-03                   19   \n",
       "2 -73.976831  40.766718      2E-AM-1010-03                   19   \n",
       "3 -73.975725  40.769703      5D-PM-1018-05                   19   \n",
       "4 -73.959313  40.797533     39B-AM-1018-01                   19   \n",
       "\n",
       "   Borough Boundaries  City Council Districts  Police Precincts  \n",
       "0                   4                      19                13  \n",
       "1                   4                      19                13  \n",
       "2                   4                      19                13  \n",
       "3                   4                      19                13  \n",
       "4                   4                      19                13  "
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[:, np.r_[0:3,location_indices]].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
